package com.example.demo.utils;

import com.aliyun.com.viapi.FileUtils;
import com.aliyun.imageprocess20200320.Client;
import com.aliyun.imageprocess20200320.models.DetectSkinDiseaseRequest;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Set;

public class DetectSkinDiseaseUtil {
//    private static final String ACCESS_KEY_ID = "LTAI5tNrd8Q6NdG5xKS6iZi2";
//    private static final String ACCESS_KEY_SECRET = "oLUGw2NS7GjXYR24dwcqlMlkTKbyvM";
    @Value("${aliyun.key_id}")
    private static String ACCESS_KEY_ID;
    @Value("${aliyun.key_secret}")
    private static String ACCESS_KEY_SECRET;
    @Value("${aliyun.end_point}")
    private static String endPoint;
    public static Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        Config config = new Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(ACCESS_KEY_ID)
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(ACCESS_KEY_SECRET);
        // Endpoint 请参考 https://api.aliyun.com/product/imageprocess
        config.endpoint = endPoint;
        return new Client(config);
    }
    public static Set<? extends Map.Entry<String, ?>> detect(MultipartFile file) throws Exception {
        Client client = createClient();
        FileUtils fileUtils = FileUtils.getInstance(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        String ossUrl = fileUtils.upload("demo",file.getInputStream());
        System.out.println(ossUrl);
        DetectSkinDiseaseRequest detectSkinDiseaseRequest = new DetectSkinDiseaseRequest()
                .setUrl(ossUrl)
                .setOrgId("0001")
                .setOrgName("demo");
        RuntimeOptions runtime = new RuntimeOptions();
        return client.detectSkinDiseaseWithOptions(detectSkinDiseaseRequest, runtime)
                .body
                .getData()
                .results
                .entrySet();
    }
}
